package com.example.service.impl;

import com.example.domain.entity.Material;
import com.example.mapper.MaterialMapper;
import com.example.service.IMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.domain.query.MaterialQuery;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2025-05-13
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {

    @Override
    public IPage<Material> pageQuery(MaterialQuery query, int pageNum, int pageSize) {
        Page<Material> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Material> wrapper = new QueryWrapper<>();
        if (query != null) {
            if (query.getMaterialId() != null) {
                wrapper.eq("material_id", query.getMaterialId());
            }
            if (StringUtils.hasText(query.getMaterialName())) {
                wrapper.like("material_name", query.getMaterialName());
            }
            if (StringUtils.hasText(query.getMaterialType())) {
                wrapper.eq("material_type", query.getMaterialType());
            }
            if (StringUtils.hasText(query.getProviderName())) {
                wrapper.like("provider_name", query.getProviderName());
            }
            if (StringUtils.hasText(query.getFactoryAddress())) {
                wrapper.like("factory_address", query.getFactoryAddress());
            }
            if (StringUtils.hasText(query.getSpecification())) {
                wrapper.eq("specification", query.getSpecification());
            }
            if (query.getStorageStartTime() != null && query.getStorageEndTime() != null) {
                wrapper.between("storage_time", query.getStorageStartTime(), query.getStorageEndTime());
            }
            if (StringUtils.hasText(query.getContactPerson())) {
                wrapper.like("contact_person", query.getContactPerson());
            }
            if (StringUtils.hasText(query.getPhone())) {
                wrapper.eq("phone", query.getPhone());
            }
            if (StringUtils.hasText(query.getDescription())) {
                wrapper.like("description", query.getDescription());
            }
        }
        return this.page(page, wrapper);
    }

}
